package text2documentvector;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.LinkedHashMap;

public class FileIO {

	private int i = 0;
	private File inFile = null;
	private File outFile = null;
	private long fileLength = 0;
	private int iFileLength = 0;
	private Reader reader = null;
	private String path2InputFile = null;
	private String path2OutputFile = null;
	private char[] inputData = null;
	private PrintWriter pWriter = null;
	
	/**
	 * Constructor for class FileIO
	 * @param path2File
	 */
	public FileIO(){		
		
	}
	
	/**
	 * Set Path to File for IO
	 * @param _path2File
	 */
	public void setPathOfInputFile(String _path2InputFile){
		path2InputFile = _path2InputFile;
	}
	
	
	
	/**
	 * Set path of output file
	 * @param _path2OutputFile
	 */
	public void setPathOfOutputFile(String _path2OutputFile){
		path2OutputFile = _path2OutputFile;
	}
	
	
	/**
	 * read from file
	 */
	public void readFile(){		
		
		inFile = new File(path2InputFile);
		fileLength = inFile.length()+1;
		iFileLength = (int)fileLength-1;
		inputData = new char[iFileLength];
		i = 0;
		
		try {
			/* Create new FileReader Object */
			reader = new FileReader(path2InputFile);
			
			/* Read data from file */
			try {
				for (int c; (c = reader.read() ) != -1;){
					inputData[i] = (char)c;
					i++;
				}
			} catch (IOException e) {
				/* Fehler beim Lesen */
				System.err.println("Die Datei konnte nicht gelesen werden!");
			}
			finally {
				try { reader.close(); } catch ( Exception e ) { }
			}
			
		} catch (FileNotFoundException e) {
			/* Datei nicht gefunden */
			System.err.println( "Die Datei wurde nicht gefunden!");
		}
	}
	
	public LinkedHashMap<String, Double> readLineLinkedHashMap(){
		LinkedHashMap<String, Double> hashmap = new LinkedHashMap<String, Double>();
		
		try {
			reader = new FileReader(path2InputFile);
			BufferedReader in = new BufferedReader(reader);
			String zeile = null;

			while ((zeile = in.readLine()) != null) {
				zeile = zeile.replace(' ', ':');
				if(zeile.split(":").length >= 2){
					hashmap.put(zeile.split(":")[0],Double.parseDouble(zeile.split(":")[1]));
				}
				else if(zeile.split(":").length == 1){
					hashmap.put(zeile.split(":")[0],0.0);
				}
				
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return hashmap;
	}
	
	/**
	 * write charArray to File
	 * @param outputData
	 */
	public void write2File(char[] outputData){
		
		i=0;
		pWriter = null;
		outFile = new File(path2OutputFile);
		if(!outFile.exists()){
			/* Create File */
			try {
				outFile.createNewFile();
			} catch (IOException e) {
				System.err.println( "Die Datei konnte nicht angelegt werden!");
			}
		}
		
		try{ 
            PrintWriter pWriter = new PrintWriter(new FileWriter(path2OutputFile)); 
            for(i=0;i<outputData.length;i++){
            	pWriter.print(outputData[i]);
            }
            pWriter.flush(); 
            pWriter.close();
        }catch(IOException ioe){ 
        	System.err.println( "Es konnte nicht in Datei geschrieben werden!"); 
        } 

	}
	
	/**
	 * Returns the Char Array of the read file
	 * @return
	 */
	public char[] getCharArrOfFile(){
		return inputData;
	}
	
	public String getStringOfFile(){
		String result = "";
		for(int i = 0; i < inputData.length; i++){
			result = result.concat(String.valueOf(inputData[i]));
		}
		return result;
	}
}
